
<script setup>
import { loginService } from '@/api/user.js'
import { ElMessage } from 'element-plus'
import { ref } from 'vue'

const loginData = ref({
  username: '',
  password: '',
  repassword: ''
})

const checkrepassword = (rule, value, callback) => {
  if (value === "") {
    callback(new Error("请在此确认密码"));
  } else if (value !== loginData.password) {
    callback(new Error("请确保再次输入的密码一样"));
  } else {
    callback();
  }
}

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 5, max: 16, message: '请输入长度为5-16位非空字符', trigger: 'blur' }  
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 5, max: 16, message: '请输入长度为5-16位非空字符', trigger: 'blur' }  
  ],
  repassword: [
    { validator: checkrepassword, trigger: 'blur' }
  ]
}
import{usetokenstore}from '@/stores/token.js'
import {useRouter} from 'vue-router'
//创建路由
const router= useRouter()
//创建token仓库
const tokenstore=usetokenstore();
const login = async () => {
  let result = await loginService(loginData.value);
 
 ElMessage.success(result.msg ? result.msg : "登录成功")

 //将token存储在pinia
 tokenstore.setToken(result.data)
router.push('/')

}
</script>




<template>

  <el-form ref="form"  :model="loginData" label-width="65px" class="loginForm" :rules="rules">
      <h3 class="login_title">登录</h3>
      <el-form-item  label="用户名" prop ='username'>
        <el-input  placeholder="请输入用户名" v-model="loginData.username" ></el-input>
      </el-form-item >
      <el-form-item label="密码" prop ='password'>
        <el-input  type="password" placeholder="请输入密码" v-model="loginData.password"></el-input>
      </el-form-item>
      <el-form-item style="width: 100%">
        <el-button type="primary" style="width: 100%;background: #505458;border: none" @click="login" >登录</el-button>
      </el-form-item>
    </el-form>
  
  </template>
  
    
  <style>
  
  .loginForm {
  width: 350px;
  margin: 120px auto;
  border: 1px solid #DCDFE6;
  padding: 20px;
  border-radius: 5px;
  box-shadow: 0 0 30px #DCDFE6;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.login_title {
  margin-bottom: 40px;
  text-align: center;
  color: #505458;
}
  </style>